﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;

namespace SHCP
{
    /// <summary>
    /// 主客户端
    /// </summary>
    public class CLT : WindowBase
    {
        public int? _price;
        public int Price
        {
            get
            {
                if (!_price.HasValue)
                {
                    this.Window.FindChild("WindowsForms10.Window.8.app.0.2bf8098_r13_ad1")
                        .FindAllChilds("WindowsForms10.STATIC.app.0.2bf8098_r13_ad1").ToList().ForEach(p =>
                        {
                            if (!_price.HasValue && p.Caption().IsMoney())
                            {
                                _price = Convert.ToInt32(p.Caption());
                            }
                        });
                }

                if (!_price.HasValue)
                    _price = -1;

                return _price.Value;
            }
        }

        public CLT(string name) : base(name) { }

        /// <summary>
        /// 出价
        /// </summary>
        public void ChuJia(bool computerPrice)
        {
            this.Active();
            if (computerPrice)
            {
                var currentPrice = this.Price + App.Default.PriceIncremental;
                Console.WriteLine("当前出价:{0}", currentPrice);
                this.Window.FindChild("WindowsForms10.Window.8.app.0.2bf8098_r13_ad1")
                       .FindAllChilds("WindowsForms10.Window.8.app.0.2bf8098_r13_ad1")
                      .Where(p => p.HasChild()).ToList().ForEach(p =>
                      {
                          Win32API.SetForegroundWindow(p.FindChild("WindowsForms10.EDIT.app.0.2bf8098_r13_ad1"));
                          SendKeys.SendWait("{BS 6}");
                          SendKeys.SendWait(currentPrice.ToString());
                          System.Threading.Thread.Sleep(500);
                      });
            }
            Click(570, 288);//确认出价
            System.Threading.Thread.Sleep(1000);
        }
    }

    public static class IntPtrExtension
    {
        public static void SetCaption(this IntPtr p, string text)
        {
            Win32API.SetWindowText(p, text);
        }

        public static string Caption(this IntPtr p)
        {
            var length = 100;
            var text = new StringBuilder(length);
            Win32API.GetWindowText(p, text, length);
            return text.ToString();
        }

        public static string ClassName(this IntPtr p)
        {
            var length = 100;
            var text = new StringBuilder(length);
            Win32API.GetClassName(p, text, length);
            return text.ToString();
        }

        public static bool HasChild(this IntPtr parent)
        {
            return parent.FindAllChilds(null).Count > 0;
        }

        public static IntPtr FindChild(this IntPtr p, string className)
        {
            return Win32API.FindWindowEx(p, IntPtr.Zero, className, null);
        }

        public static IList<IntPtr> FindAllChilds(this IntPtr parent, string className)
        {
            if (string.IsNullOrEmpty(className))
                return GetChildWindows(parent);
            return GetChildWindows(parent).Where(p => p.ClassName() == className).ToList();
        }

        #region
        private static List<IntPtr> GetChildWindows(IntPtr parent)
        {
            List<IntPtr> result = new List<IntPtr>();
            GCHandle listHandle = GCHandle.Alloc(result);
            try
            {
                SHCP.Win32API.EnumWindowProc childProc = new SHCP.Win32API.EnumWindowProc(EnumWindow);
                SHCP.Win32API.EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
            }
            finally
            {
                if (listHandle.IsAllocated)
                    listHandle.Free();
            }
            return result;
        }

        private static bool EnumWindow(IntPtr handle, IntPtr pointer)
        {
            GCHandle gch = GCHandle.FromIntPtr(pointer);
            List<IntPtr> list = gch.Target as List<IntPtr>;
            if (list == null)
                throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");

            list.Add(handle);
            return true;
        }
        #endregion
    }
}